﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Security.Permissions;
using System.Drawing.Design;
using Org.Eurekaa.DNJ.Exceptions;


namespace Org.Eurekaa.DNJ.Collections
{
    [Editor("System.Web.UI.Design.WebControls.ListItemsCollectionEditor, System.Design, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor)), PermissionSet(SecurityAction.LinkDemand, XML = "<PermissionSet class=\"System.Security.PermissionSet\"\r\n               version=\"1\">\r\n   <IPermission class=\"System.Web.AspNetHostingPermission, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\"\r\n                version=\"1\"\r\n                Level=\"Minimal\"/>\r\n</PermissionSet>\r\n")]
    public class DNJGenericCollection<T> : CollectionBase
    {
        #region Constructor
        public DNJGenericCollection()
        {
        }
        #endregion Constructor

        #region Indexer        
        public T this[int i]
        {
            get
            {
                if (i < 0 || i >= this.Count) return default(T);
                    //throw new OutOfRangeException();

                return (T)this.List[i];
            }
            set
            {
                this.List[i] = value;
            }
        }

        public T this[object index]
        {
            get
            {

                int ind = IndexOf(index);
                if (ind < 0)
                    throw new OutOfRangeException();

                return (T)this.List[ind];
            }
            set
            {
                this.List[IndexOf(index)] = value;
            }
        }
        #endregion Indexer

        #region Public Methods
        public void Add(T item)
        {
            this.List.Add(item);
        }


        public void Insert(int index, T item)
        {
            this.List.Insert(index, item);
        }

        public void Remove(T item)
        {
            this.List.Remove(item);
        }

        public bool Contains(T item)
        {
            return this.List.Contains(item);
        }

        //Collection IndexOf method 

        /// <summary>
        /// Find index of control based on ID
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public int IndexOf(object obj)
        {
            if (obj is int)
                return (int)obj;

            if (obj is string)
            {
                for (int i = 0; i < List.Count; i++)
                {
                    if (i.ToString().ToUpper() == obj.ToString().ToUpper())
                        return i;
                }
                return -1;
            }
            else
            {
                throw new InvalidIndexValueException();
            }
        }

        public void CopyTo(T[] array, int index)
        {
            List.CopyTo(array, index);
        }

        public bool Contains(string key)
        {
            return List.Contains(key);
        }

        public void Remove(string key)
        {
            List.Remove(key);
        }
        #endregion Public Methods
    }

}
